package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.LoginCheck;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.MobileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Zty
 * @Date 2023/9/3 11:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/mobile")
@Slf4j
public class MobileController {
    @Autowired
    private MobileService mobileService;
    @Autowired
    private MemberService memberService;

    /**
     * 登陆短信验证
     *
     * @param type
     * @param telephone
     * @return
     */
    @PostMapping("/validateCode/send")
    public Result checkCode(String type, String telephone) {
        mobileService.checkCode(type,telephone);

        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 验证码校验
     * @param loginCheck
     * @return
     */
    @PostMapping("/login/smsLogin")
    public Result login(@RequestBody LoginCheck loginCheck){
       Boolean flag = mobileService.loginCheck(loginCheck,"MOBILE_SIGNIN");

       if (!flag){
           return new Result(false,MessageConst.VALIDATECODE_ERROR);
       }
        Member member= new Member();
       member.setPhoneNumber(loginCheck.getTelephone());
        member = memberService.createOrUpdate(member);
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }
}
